<template>
    <div>
        <h2>登录</h2>
        <table>
            <tbody>
                <tr>
                    <td>登录</td>
                    <td>
                        <input v-model="data.name" type="text">
                    </td>
                </tr>
                <tr>
                    <td>密码</td>
                    <td>
                        <input v-model="data.pwd" type="password">
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="button" value="登录" @click="login">
                    </td>
                </tr>
            </tbody>
        </table>  
    </div>
</template>

<script setup lang="ts">

import axios from 'axios';
import { ref } from 'vue';

import { useRouter } from 'vue-router';

import{ useMemberStore } from '@/stores/user' //引入pinia对象
const user = useMemberStore(); //实例化
const router = useRouter();
    const data=ref({
        name:'',
        pwd:''
    })

    const login=()=>{
        axios.get("https://localhost:7174/api/Member/Login",{params:data.value}).then(res=>{
            if(res.data.id==-1){
                alert('登录名不存在')
                return;
            }
            if(res.data.id==-2){
                alert('密码不错误')
                return;
            }
            if(res.data.id > 0){
                debugger
                alert("登录成功")
                //保存登录状态，使用Pinia来保存登录状态信息
                user.userinfo.username = data.value.name;
                user.userinfo.islogin=true;
                router.push({path:"/"})
            }

        })
    }

</script>

<style scoped>

</style>